// 导入静态路由和动态路由
import { constantRoutes, asyncRoutes } from '@/router/index'

const state = {
  routes: constantRoutes   //路由表默认等于静态路由
}
const mutations = {
  setRoutes(state, newRoutes) {
    // 将静态路由和 动态路由合并成路由表
    state.routes = [...constantRoutes, ...newRoutes]
  }
}

const actions = {
  // 筛选出符合的动态路由表
  filterPermission(context, menus) {
    const routes = asyncRoutes.filter(item => menus.includes(item.name))
    context.commit('setRoutes', routes) // 触发mutations中 setRoutes
    return routes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}